<template>
    <div>
        添加产品
        <van-field label="请输入名称" placeholder="名称" v-model="form.name"/>
        <van-field label="请输入价格" placeholder="价格" v-model="form.price"/>
        <select v-model="form.city_id">
            <option :value="i.id" v-for="i in list">{{i.name}}</option>
        </select>
        上传图片<van-uploader :after-read="upload"/>
        <van-button @click="add">添加</van-button>
    </div>
</template>

<script>
    export default {
        data(){
            return{
                form:{pic:'',city_id:0},
                list:[]
            }
        },
        methods:{
            //获取城市列表
            get_list(){
                this.$axios.get('pic/cityM')
                .then(res=>{
                    if(res.data.code==200){
                        this.list=res.data.list
                        //默认选中第一个
                        this.form.city_id=this.list[0]['id']
                    }
                })
            },
            upload(file){
                //图片上传操作
                let formdata=new FormData()
                formdata.append('file',file.file)
                formdata.append('city_id',this.form.city_id)
                this.$axios.post('pic/upload',formdata)
                .then(res=>{
                    if(res.data.code==200){
                        alert(res.data.url)
                        this.form.pic=res.data.url
                    }
                })
            },
            add(){
                //只要是上传文件就需要声明formdata
               
                this.$axios.post('pic/produatA',this.form)
                .then(res=>{
                    if(res.data.code==200){
                        alert('添加成功')
                    }
                })
            },
        },
        mounted(){
            this.get_list()
        },
    }
</script>

<style scoped>

</style>